package form.ui.core {
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormatAlign;
	
	import form.core.StyleManager;
	import form.ui.component.Layout.Box;
	import form.ui.component.Layout.LayoutContainer;
	
	public class Button extends Container 
	{	
		public var upSkin:String;
		public var overSkin:String;
		public var downSkin:String;
		public var disabledSkin:String;
		private var _mouseUp:DisplayObject;
		private var _mouseOver:DisplayObject;
		private var _mouseDown:DisplayObject;
		private var _disabled:DisplayObject;
		
		private var _clickable:Boolean = false;
		
		private var enabled:Boolean = false;
		
		public function Button() {
			super();
			
			this.mouseChildren = false;
			this.clickable = true;
			this.centerLabel = true;
			
			this.registerStyle("upSkin");
			this.registerStyle("overSkin");
			this.registerStyle("downSkin");
			this.registerStyle("disabledSkin");
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
			this.addEventListener(MouseEvent.ROLL_OVER, this.onMouseOver);
			this.addEventListener(MouseEvent.ROLL_OUT, this.onMouseOut);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			
			
		}
		
		public function mouseDownAction():void {	
		}
		
		public function mouseUpAction():void {	
		}
		
		public function enable():void {
			if (!this.enabled) {
				this.enabled = true;
				this.showMouseUp();
			}
		}
		
		public function disable():void {
			if (this.enabled) {
				this.enabled = false;
				this.showDisabled();
			}
		}
		
		public function set mouseUp(object:DisplayObject):void {
			if (this.mouseUp != null) {
				this.removeChild(this.mouseUp);
			}
			
			this._mouseUp = object;
			if(object)
				this.addChild(this.mouseUp);
		}
				
		public function get mouseUp():DisplayObject {
			return this._mouseUp;
		}
				
		public function set mouseOver(object:DisplayObject):void {
			if (this.mouseOver != null) {
				this.removeChild(this.mouseOver);
			}
			
			this._mouseOver = object;
			if(object)
			this.addChild(this.mouseOver);
		}
		
		public function get mouseOver():DisplayObject {
			return this._mouseOver;
		}
		
		public function set mouseDown(object:DisplayObject):void {
			if (this.mouseDown != null) {
				this.removeChild(this.mouseDown);
			}
			
			this._mouseDown = object;
			if(object)
			this.addChild(this.mouseDown);
		}
		
		public function get mouseDown():DisplayObject {
			return this._mouseDown;
		}
		
		public function set disabled(object:DisplayObject):void {
			if (this.disabled != null) {
				this.removeChild(this.disabled);
			}
			
			this._disabled = object;
			if(object)
			this.addChild(this.disabled);
		}
		
		public function get disabled():DisplayObject {
			return this._disabled;
		}
		
		protected function showMouseUp():void {
			this.clickable = true;
			
			if (this.mouseUp != null) { this.mouseUp.visible = true; }
			if (this.mouseOver != null) { this.mouseOver.visible = false; }
			if (this.mouseDown != null) { this.mouseDown.visible = false; }
			if (this.disabled != null) { this.disabled.visible = false; }
		}
		
		protected function showMouseOver():void {
			this.clickable = true;
			
			if (this.mouseUp != null) { this.mouseUp.visible = false; }
			if (this.mouseOver != null) { this.mouseOver.visible = true; }
			if (this.mouseDown != null) { this.mouseDown.visible = false; }
			if (this.disabled != null) { this.disabled.visible = false; }
		}
		
		protected function showMouseDown():void {
			this.clickable = true;
			
			if (this.mouseUp != null) { this.mouseUp.visible = false; }
			if (this.mouseOver != null) { this.mouseOver.visible = false; }
			if (this.mouseDown != null) { this.mouseDown.visible = true; }
			if (this.disabled != null) { this.disabled.visible = false; }
		}
		
		protected function showDisabled():void {
			this.clickable = false;
			
			if (this.mouseUp != null) { this.mouseUp.visible = false; }
			if (this.mouseOver != null) { this.mouseOver.visible = false; }
			if (this.mouseDown != null) { this.mouseDown.visible = false; }
			if (this.disabled != null) { this.disabled.visible = true; }
		}
		
		private function onMouseDown(e:Event):void {
			if (!this.enabled) { return; }
			
			this.showMouseDown();
			
			this.mouseDownAction();
		}
		
		private function onMouseUp(e:Event):void {
			if (!this.enabled) { return; }
			
			this.showMouseOver();
			
			this.mouseUpAction();
		}
		
		private function onMouseOver(e:Event):void {
			if (!this.enabled) { return; }
			
			this.showMouseOver();
		}
		
		protected function onMouseOut(e:Event):void {
			if (!this.enabled) { return; }
			
			this.showMouseUp();
		}
		
		protected function onAddedToStage(e:Event):void {
			this.enable();
		}
		
		protected function set clickable(value:Boolean):void {
			this._clickable = value;
			
			this.mouseEnabled = this.clickable;
			this.buttonMode = this.clickable;
			this.useHandCursor = this.clickable;	
		}
		
		protected function get clickable():Boolean {
			return this._clickable;
		}
		
		override public function updateStyle():void
		{
			super.updateStyle();
			if(upSkin && upSkin != "")
			{
				mouseUp = StyleManager.getInstance().getStyleDefinition(upSkin);
			}
			if(overSkin && overSkin != "")
			{
				mouseOver = StyleManager.getInstance().getStyleDefinition(overSkin);
			}
			if(downSkin && downSkin != "")
			{
				mouseDown = StyleManager.getInstance().getStyleDefinition(downSkin);
			}
			if(disabledSkin && disabledSkin != "")
			{
				disabled = StyleManager.getInstance().getStyleDefinition(disabledSkin);
			}
			this.enabled ? this.showMouseUp() : this.disable();
			
			if(this.parent is LayoutContainer)
			{
				LayoutContainer(this.parent).invalidateLayout();
			}
			else if(this.parent is Box)
			{
				Box(this.parent).invalidateLayout();
			}
		}
		
		private var _centerLabel:Boolean;
		private function set centerLabel(value:Boolean):void
		{
			_centerLabel = value;
			draw();
		}
		private function get centerLabel():Boolean
		{
			return _centerLabel;
		}
		
		override public function draw():void
		{
			super.draw();
			if(_centerLabel)
			{
				this.labelField.width = this.width;
				this.labelField.textAlign = TextFormatAlign.CENTER;
			}
			else
			{
				this.labelField.textAlign = TextFormatAlign.LEFT;
				this.labelField.x = this.labelLeft;
			}
		}
		
	}
}